﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using BW.WMS.WareHouseAllot;
using BW.WMS.WareHouseAllot.Dto;
using BW.WMS.WareHouseAllot.Parameter.Input.GoodsType;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;

namespace BW.WMS.RabbitMQ
{
    public class MyRabbitMQ : IDistributedEventHandler<AddGoodsTypeInput>, ITransientDependency
    {
        private readonly IRepository<GoodsTypeModel, Guid> goodsTypeRepository;
        private readonly IMapper mapper;

        public MyRabbitMQ(IRepository<GoodsTypeModel, Guid> GoodsTypeRepository,IMapper mapper)
        {
            goodsTypeRepository = GoodsTypeRepository;
            this.mapper = mapper;
        }

        public async Task HandleEventAsync(AddGoodsTypeInput input)
        {
            var goodsType = mapper.Map<GoodsTypeModel>(input);
            //编号等于 GT + 当前时间的年月日 +从0000开始递增的数字
            goodsType.GoodsTypeCode = "GT" + DateTime.Now.ToString("yyyyMMdd") + (await goodsTypeRepository.GetCountAsync()).ToString("D4");

            //throw new Exception("牛逼");
            await goodsTypeRepository.InsertAsync(goodsType);
        }
    }
}
